using System;
using System.Diagnostics;
using System.Text;

namespace GADemo
{
    public partial class TraceForm : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public TraceForm()
        {
            InitializeComponent();
            CreateListener();
        }

        private void CreateListener()
        {
            //TextWriterTraceListener listener = new TextWriterTraceListener();
            //TextWriter writer = TextWriter.
            //textBox1.Text
            listener = new TextBoxTraceListener();
            Trace.Listeners.Clear();
            Trace.Listeners.Add(listener);
            timer.Start();
        }

        private TextBoxTraceListener listener;

        private class TextBoxTraceListener : TraceListener
        {
            //public TextBoxTraceListener(TextBox txb)
            //{
            //    this.txb = txb;
            //    txb.Invalidated += new InvalidateEventHandler(txb_Invalidated);
            //}

            //void txb_Invalidated(object sender, InvalidateEventArgs e)
            //{
            //    /*
            //    if (builder.length > 0)
            //    {
            //        txb.Text += builder.ToString();
            //        builder.length = 0;
            //    }
            //     */
            //}
            
            //TextBox txb;

            StringBuilder builder = new StringBuilder();

            public override void Write(string message)
            {
                builder.Append(message);
            }

            public override void WriteLine(string message)
            {
                builder.AppendLine(message);
            }

            public void Clear()
            {
                builder.Length = 0;
            }

            public string GetText()
            {
                return builder.ToString();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            txbTracing.Text += listener.GetText();
            listener.Clear();
        }
    }

    [Flags]
    public enum TraceMode
    {
        None = 0,
        Stats = 1,
        Conditions = 2,
        Selection = 4,
        Crossover = 8,
        Mutation = 16,
        Merge = 32,
        Evolution = TraceMode.Stats | TraceMode.Conditions,
        Operations = TraceMode.Selection | TraceMode.Crossover | TraceMode.Mutation | TraceMode.Merge,
        Verbose = TraceMode.Evolution | TraceMode.Operations
    }
}

